package xcmg.device.service.delivery;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.delivery.LogisticsBasicDataDO;
import xcmg.device.dao.entity.delivery.LogisticsBasicWeightDO;
import xcmg.device.dao.mapper.delivery.LogisticsBasicDataMapper;
import xcmg.device.dao.mapper.delivery.LogisticsBasicWeightMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.delivery.LogisticsBasicDataVO;
import yb.ecp.fast.dto.UserCacheDTO;
import yb.ecp.fast.feign.AuthClient;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
/**
 * 物流基础数据维护
 * @author huhuawei
 */
public class LogisticsBasicDataService
{


    @Autowired
    private LogisticsBasicDataMapper logisticsBasicDataMapper;

    @Autowired
    private LogisticsBasicWeightMapper logisticsBasicWeightMapper ;

    @Autowired
    private FastGenClient fastGenClient;

    @Autowired
    private AuthClient authClient;


    /**
     * 增加物流基础信息
     * （根据基础信息增加重量区间信息）
     * @param logisticsBasicDataVO
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public ErrorCode add(LogisticsBasicDataVO logisticsBasicDataVO) throws Exception
    {
        ActionResult<String> textNote = fastGenClient.textGuid();
        if (textNote.getCode() != ErrorCode.Success.getCode())
        {
            return ErrorCode.IllegalArument;
        }
        //查询同一承运商 出发城市  目的城市的唯一性
        LogisticsBasicDataVO validate=new LogisticsBasicDataVO();
        validate.setCarrierOrgId(logisticsBasicDataVO.getCarrierOrgId());
        validate.setDepartureCity(logisticsBasicDataVO.getDepartureCity());
        validate.setDestinationCity(logisticsBasicDataVO.getDestinationCity());
        List<LogisticsBasicDataDO> doList = logisticsBasicDataMapper.list(validate);
        if(doList.size()>0){
            return ErrorCode.DeliveryLogisticsBasicDataNote;
        }
        ActionResult userAuth =  authClient.getUserDetail(logisticsBasicDataVO.getCreateId());
        UserCacheDTO userInfo = (UserCacheDTO) userAuth.getValue();
        String basicUuid = textNote.getValue().toString();
        logisticsBasicDataVO.setId(basicUuid);
        logisticsBasicDataVO.setCreateId(userInfo.getUserId());
        logisticsBasicDataVO.setCreateName(userInfo.getName());
        logisticsBasicDataVO.setCreateDate(new Date());
        logisticsBasicDataVO.setUpdateId(userInfo.getUserId());
        logisticsBasicDataVO.setUpdateDate(new Date());

        logisticsBasicDataMapper.add(logisticsBasicDataVO);

        //根据物流信息增加重量区间信息
        List<LogisticsBasicWeightDO> basicWeightList = logisticsBasicDataVO.getList();
        for(LogisticsBasicWeightDO logisticsBasicWeightDO:basicWeightList){
            //生成当前的UUID
            ActionResult<String> textDetail = fastGenClient.textGuid();
            logisticsBasicWeightDO.setId(textDetail.getValue().toString());
            //存储basicId信息
            logisticsBasicWeightDO.setBasicId(basicUuid);
            logisticsBasicWeightMapper.add(logisticsBasicWeightDO);
        }
        return ErrorCode.Success;
    }

    /**
     * 删除物流基础信息
     * 支持多个
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ErrorCode remove(List<String> ids)
    {
        for (String id : ids)
        {
            logisticsBasicDataMapper.deleteByPrimaryKey(id);

            //根据basicId删除重量区间信息
            logisticsBasicWeightMapper.deleteByBasicKey(id);
        }
        return ErrorCode.Success;
    }

    /**
     * 更新物流基础信息
     * @param logisticsBasicDataVO
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public ErrorCode update(LogisticsBasicDataVO logisticsBasicDataVO) throws Exception
    {
        //查询同一承运商 出发城市  目的城市的唯一性
        LogisticsBasicDataVO validate=new LogisticsBasicDataVO();
        validate.setId(logisticsBasicDataVO.getId());
        validate.setCarrierOrgId(logisticsBasicDataVO.getCarrierOrgId());
        validate.setDepartureCity(logisticsBasicDataVO.getDepartureCity());
        validate.setDestinationCity(logisticsBasicDataVO.getDestinationCity());
        //更新时候校验  不包括自己
        int count = logisticsBasicDataMapper.countByCondition(validate);
        if(count > 0){
            return ErrorCode.DeliveryLogisticsBasicDataNote;
        }

        ActionResult userAuth =  authClient.getUserDetail(logisticsBasicDataVO.getCreateId());
        UserCacheDTO userInfo = (UserCacheDTO) userAuth.getValue();
        logisticsBasicDataVO.setUpdateId(userInfo.getUserId());
        logisticsBasicDataVO.setUpdateDate(new Date());

        logisticsBasicDataMapper.update(logisticsBasicDataVO);

        //根据物流基础表信息，先删除重量区间表信息
        logisticsBasicWeightMapper.deleteByBasicKey(logisticsBasicDataVO.getId());
        //再根据basicId重新添加
        List<LogisticsBasicWeightDO> basicWeightList = logisticsBasicDataVO.getList();
        for(LogisticsBasicWeightDO logisticsBasicWeightDO:basicWeightList){
            ActionResult<String> textUpdate = fastGenClient.textGuid();
            logisticsBasicWeightDO.setId(textUpdate.getValue().toString());
            logisticsBasicWeightDO.setBasicId(logisticsBasicDataVO.getId());
            logisticsBasicWeightMapper.add(logisticsBasicWeightDO);
        }
        return ErrorCode.Success;
    }

    /**
     * 物流基础信息列表
     * @param condition
     * @return
     */
    public PageCommonVO list(SearchCommonVO<LogisticsBasicDataVO> condition) {
        PageCommonVO result = new PageCommonVO();
        PageHelperPlus.orderBy("create_date desc");
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<LogisticsBasicDataDO> doList = logisticsBasicDataMapper.list(condition.getFilters());
        List<LogisticsBasicDataVO> voList = new ArrayList<>();
        for (LogisticsBasicDataDO entity : doList) {
            LogisticsBasicDataVO model = new LogisticsBasicDataVO();
            BeanUtils.copyProperties(entity, model);
            //根据物流基础信息查询重量区间信息
            List<LogisticsBasicWeightDO> detailList = logisticsBasicWeightMapper.list(model.getId());
            model.setList(detailList);
            voList.add(model);
        }
        result.setPageInfo(new PageInfo(doList));
        result.setPageInfoList(voList);
        return result;
    }

    /**
     * 根据id查询单个的物流信息
     * @param id
     * @return
     */
    public LogisticsBasicDataVO item(String id)
    {
        LogisticsBasicDataDO doEntity = logisticsBasicDataMapper.item(id);
        LogisticsBasicDataVO model = new LogisticsBasicDataVO();
        BeanUtils.copyProperties(doEntity, model);
        //根据物流基础信息查询重量区间信息
        List<LogisticsBasicWeightDO> detailList=logisticsBasicWeightMapper.list(model.getId());
        model.setList(detailList);
        return  model;
    }
}

